package com.ksr.logistics.controller;

import com.github.pagehelper.PageInfo;
import com.ksr.logistics.pojo.BaseData;
import com.ksr.logistics.pojo.Result;
import com.ksr.logistics.service.BaseDataService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/baseData")
public class BaseDataController {

    @Autowired
    private BaseDataService baseDataService;

    @RequestMapping("/list")
    @RequiresPermissions("baseData:list")
    @ResponseBody
    public PageInfo<BaseData> show(Integer pageNum, Integer pageSize, String keyword){
        List<BaseData> datas=baseDataService.selPageBaseData(pageNum,pageSize,keyword);
        PageInfo<BaseData> infos=new PageInfo<>(datas);
        if (infos.getSize()==0 && infos.getPageNum()>1){
            datas=baseDataService.selPageBaseData(pageNum-1,pageSize,keyword);
            infos=new PageInfo<>(datas);
        }
        return infos;
    }

    @RequestMapping("/delete")
    @RequiresPermissions("baseData:delete")
    @ResponseBody
    public Result delete(Long id){
        return baseDataService.deleteById(id);
    }

    @RequestMapping("/deletes")
    @RequiresPermissions("baseData:deletes")
    @ResponseBody
    public Result deletes(@RequestParam("ids[]") List<Long> ids){
        return baseDataService.deleteByIds(ids);
    }

    @RequestMapping("/edit")
    @RequiresPermissions("baseData:edit")
    public String baseDataEdit(Model m,Long baseId){
        if (null!=baseId){
            BaseData baseData=baseDataService.selById(baseId);
            m.addAttribute("baseData",baseData);
        }
       List<BaseData> baseDatas=baseDataService.selParentData();
       m.addAttribute("baseDatas",baseDatas);
       return "baseDataEdit";
    }

    @RequestMapping("/insert")
    @RequiresPermissions("baseData:insert")
    @ResponseBody
    public Result insert(BaseData baseData){
        Result result=new Result(0,"新增数据失败,请重试！");
        int row=baseDataService.insertBaseData(baseData);
        if (1==row){
            result.setCode(1);
            result.setMsg("新增数据成功！");
        }

        return result;
    }

    @RequestMapping("/checkBaseDataName")
    @RequiresPermissions("baseData:checkBaseDataName")
    @ResponseBody
    public boolean checkBaseDataName(String baseName,Long baseId){
        boolean res=true;
        if (null!=baseId){
            BaseData d=baseDataService.selById(baseId);
            if (baseName.equals(d.getBaseName())){
                return true;
            }
        }
        List<BaseData> list=baseDataService.selByBaseName(baseName);
        if (list.size()>0){
            res=false;
        }
        return res;
    }

    @RequestMapping("/update")
    @RequiresPermissions("baseData:update")
    @ResponseBody
    public Result update(BaseData data){
        Result result=new Result(0,"数据内容修改失败,请重试！");
        int row=baseDataService.updateBaseData(data);
        if (1==row){
            result.setCode(1);
            result.setMsg("数据内容修改成功！");
        }

        return result;
    }

}
